<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Common\Constant\Status;
use App\Services\IPayService;
use App\Services\ISystemService;
use App\Services\IWalletService;
use App\Services\IWeinxinQyService;
use Illuminate\Http\Request;
use Log;

/**
 * 支付网关
 * Class PayController
 * @package App\Http\Controllers
 */
class PayController extends BaseController
{

    /**
     * 生成第三方支付表单，待支付
     * @param Request $request
     */
    public function form(Request $request,IPayService $payService,IWalletService $walletService,ISystemService $systemService)
    {
        if ($request->isMethod('post') && $request->has('amount') && $request->has('pay_method') && $request->has('trade_type')){
            if ($request->input('pay_method') == Params::PAY_METHOD_BALANCE){
                if (!$walletService->issetPassword(getCurrentUserid())){
                    $systemService->alert('未设置钱包密码，请先设置钱包密码');
                    return redirect('wallet/password');
                }
            }

            $payService->setPaymentMethod($request->input('pay_method'));

            switch ($request->input('trade_type')){
                case Params::TRADE_TYPE_ASSURE:
                    return $payService->assureFundOrderForm(getCurrentUserid(),$request->input('amount'),$request->input('redirectUrl',url('wallet/assurefund')));
            }
            return $this->renderFail(Status::PAYMENT_TRADE_TYPE_NOTSET);
        }
        return $this->renderFail(Status::PAYMENT_FORM_WRONG);
    }
}
